package controllers

import (
	"ecomserver/models"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"github.com/gomodule/redigo/redis"
	"strconv"
)

type CartController struct {
	beego.Controller
}

func (this *CartController) HandleAddCart(){
	resp := make(map[string]interface{})
	defer this.ServeJSON()
	skuid, err1 := this.GetInt("skuid")
	count, err2 := this.GetInt("count")
	if err1 != nil || err2 != nil{
		resp["code"] = 1
		resp["msg"]="传递数据不正确"
		this.Data["json"] = resp
		return
	}
	userName := GetUser(&this.Controller)
	if userName == ""{
		resp["code"] = 2
		resp["msg"]="未登陆"
		this.Data["json"] = resp
		return
	}
	o := orm.NewOrm()
	var user models.User
	user.Name = userName
	err := o.Read(&user, "Name")
	if err != nil{
		beego.Info(err)
		return
	}

	//购物车存储在redis中,用hash
	conn, err := redis.Dial(models.GetRedisAddr())

	if err != nil{
		beego.Info("数据库连接错误", err)
		return
	}
	defer conn.Close()

	preCount, err := redis.Int(conn.Do("hget", "cart_" + strconv.Itoa(user.Id), skuid))
	conn.Do("hset", "cart_" + strconv.Itoa(user.Id), skuid, preCount + count)

	cartCount,err := redis.Int(conn.Do("hlen", "cart_" + strconv.Itoa(user.Id)))


	resp["code"] = 5
	resp["msg"] = "OK"
	resp["cartCount"] = cartCount
	this.Data["json"] = resp

}

func (this *CartController) ShowCart(){
	username := GetUser(&this.Controller)

	conn, err := redis.Dial(models.GetRedisAddr())
	if err != nil{
		beego.Info(err)
		return
	}
	defer conn.Close()

	o := orm.NewOrm()
	var user models.User
	user.Name = username
	err = o.Read(&user, "Name")
	if err != nil{
		beego.Info(err)
		return
	}
	goodsmap, _ := redis.IntMap(conn.Do("hgetall", "cart_" + strconv.Itoa(user.Id)))

	goods := make(map[int]interface{}, len(goodsmap))

	i := 0
	totalPrice := 0
	totalCount := 0
	for key, val := range goodsmap{
		var good models.GoodsSKU
		good.Id , err = strconv.Atoi(key)
		err := o.Read(&good)
		if err != nil{
			continue
		}

		temp := make(map[string]interface{}, 2)
		temp["goodsSku"] = good
		temp["count"] = val
		temp["allPrice"] = val * good.Price
		totalCount += val
		totalPrice += good.Price * val
		goods[i] = temp
		i += 1
	}
	this.Data["totalPrice"] = totalPrice
	this.Data["totalCount"] = totalCount

	this.Data["goods"] = goods
	this.Data["goodsSize"] = len(goods)
 	this.TplName = "cart.html"
}

func (this *CartController) UpdateCart(){
	resp := make(map[string]interface{})
	defer this.ServeJSON()

	skuid, err1 := this.GetInt("skuid")
	count, err2 := this.GetInt("count")
	if err1 != nil || err2 != nil{
		resp["code"] = 1
		resp["msg"] = "err1err2"
		this.Data["json"] = resp
		return
	}

	username := this.GetSession("username")
	if username == nil{
		resp["code"] = 3
		resp["msg"] = "当前用户未登陆"
		this.Data["json"] = resp
		return
	}
	o := orm.NewOrm()
	var user models.User
	user.Name = username.(string)
	err := o.Read(&user, "Name")
	if err != nil{
		resp["code"] = 6
		resp["msg"] = "当前用户不存在"
		this.Data["json"] = resp
		return
	}


	conn, err := redis.Dial(models.GetRedisAddr())

	if err != nil{
		resp["code"] = 4
		resp["msg"] = err
		this.Data["json"] = resp
		return
	}
	defer conn.Close()
	conn.Do("hset", "cart_" + strconv.Itoa(user.Id), skuid, count)

	resp["code"] = 5
	resp["msg"] = "OK"
	this.Data["json"] = resp
}

func (this *CartController) DeleteCart(){
	resp := make(map[string]interface{})
	defer this.ServeJSON()

	skuid, err := this.GetInt("skuid")
	if err != nil{
		resp["code"] = 1
		resp["msg"] = "err skuid"
		this.Data["json"] = resp
		return
	}
	username := this.GetSession("username")
	if username == nil{
		resp["code"] = 3
		resp["msg"] = "当前用户未登陆"
		this.Data["json"] = resp
		return
	}
	o := orm.NewOrm()
	var user models.User
	user.Name = username.(string)
	err = o.Read(&user, "Name")
	if err != nil{
		resp["code"] = 6
		resp["msg"] = "当前用户不存在"
		this.Data["json"] = resp
		return
	}
	conn, err := redis.Dial(models.GetRedisAddr())
	if err != nil{
		resp["code"] = 4
		resp["msg"] = err
		this.Data["json"] = resp
		return
	}
	defer conn.Close()
	conn.Do("hdel", "cart_" + strconv.Itoa(user.Id), skuid)
	carLen, err := redis.Int(conn.Do("hlen", "cart_" + strconv.Itoa(user.Id)))

	resp["carLen"] = carLen
	resp["code"] = 5
	resp["msg"] = "OK"
	this.Data["json"] = resp
}